package net.siufung.market.base.user.controller.model.dto;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.AbstractDto;
import net.siufung.core.model.consts.Constants;
import net.siufung.core.model.consts.RegexPool;
import net.siufung.core.utils.ValidationUtil;
import net.siufung.core.utils.ValidationUtil.ErrorMessage;
import net.siufung.core.utils.ValidationUtil.ValidResult;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author 陈建峰
 * @since 2022/6/7 9:24 上午
 */
@Slf4j
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel
public class UserUpdatePasswdDto extends AbstractDto {

    private static final long serialVersionUID = 2759608479999443821L;

    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID")
    @NotNull(message = "主键ID不能为空!")
    private Integer id;

    /**
     * 在用密码
     */
    @ApiModelProperty("在用密码")
    @Length(max=32,message = "在用密码长度不能超过32")
    @NotBlank(message = "在用密码不能为空！")
    private String usedPassword;

    /**
     * 新密码
     */
    @ApiModelProperty("新密码")
    @NotBlank(message = "新密码不能为空！")
    private String newPassword;

    /**
     * 确认密码
     */
    @ApiModelProperty("确认密码")
    @NotBlank(message = "确认密码不能为空！")
    private String confirmPassword;

    @Override
    public ValidResult validResult() {
        ValidResult validResult = ValidationUtil.validateBean(this);
        if(!ReUtil.isMatch(RegexPool.LOGIN_PASSWORD, newPassword)){
            validResult.addError(new ErrorMessage("newPassword", RegexPool.LOGIN_PASSWORD_TIP));
        }
        if(!StrUtil.equals(newPassword, confirmPassword)){
            validResult.addError(new ErrorMessage("confirmPassword", "新密码和确认密码不能为空"));
        }
        if(StrUtil.equals(newPassword, Constants.INIT_PASSWORD)){
            validResult.addError(new ErrorMessage("newPassword", "新密码不能等于初始化密码"));
        }
        if(StrUtil.equals(usedPassword, newPassword)){
            validResult.addError(new ErrorMessage("usedPassword", "新密码不能等于在用密码"));
        }
        return validResult;
    }
}
